﻿/*

  The contents of this file are subject to the Mozilla Public License Version
  1.1 (the "License"); you may not use this file except in compliance with
  the License. You may obtain a copy of the License at 
  
           http://www.mozilla.org/MPL/ 
  
  Software distributed under the License is distributed on an "AS IS" basis,
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  for the specific language governing rights and limitations under the License. 
  
  The Original Code is AST'r Framework.
  
  The Initial Developer of the Original Code is
  ALCARAZ Marc (aka eKameleon) <ekameleon@gmail.com>.
  Portions created by the Initial Developer are Copyright (C) 2004-2011
  the Initial Developer. All Rights Reserved.
  
  Contributor(s) :
  
*/
package evideo 
{
    import core.reflect.getClassPackage;

    import evideo.controllers.display.FullScreen;
    import evideo.controllers.display.SwitchFullScreen;
    import evideo.controllers.display.VideoOut;
    import evideo.controllers.display.VideoOver;
    import evideo.controllers.models.videos.AddVideo;
    import evideo.controllers.models.videos.ChangeVideo;
    import evideo.controllers.net.MetaDataChange;
    import evideo.controllers.net.MuteStream;
    import evideo.controllers.net.PauseStream;
    import evideo.controllers.net.PlayStream;
    import evideo.controllers.net.ProgressStream;
    import evideo.controllers.net.SeekStream;
    import evideo.controllers.net.StatusStream;
    import evideo.controllers.net.UnmuteStream;
    import evideo.display.VideoList;
    import evideo.display.application.Body;
    import evideo.display.application.Footer;
    import evideo.display.application.VideoProtector;
    import evideo.display.application.Wallpaper;
    import evideo.logging.sos;
    import evideo.process.InitApplication;

    import graphics.FillStyle;
    import graphics.LineStyle;

    import lunas.components.buttons.DashboardButton;
    import lunas.components.buttons.LightFrameLabelButton;
    import lunas.containers.AutoScrollContainer;

    import system.evaluators.EdenEvaluator;
    import system.events.FrontController;
    import system.logging.LoggerLevel;
    import system.logging.targets.SOSTarget;
    import system.process.Action;

    import vegas.display.Root;
    import vegas.models.maps.MapModelObject;
    import vegas.net.ApplicationLoader;
    import vegas.net.NetServerConnection;
    import vegas.net.NetStreamExpert;

    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.Event;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
    import flash.text.TextFormat;
    
    [SWF(width="980", height="720", frameRate="24", backgroundColor="#FFFFFF")]
    // -default-size 980 720 -default-frame-rate 31 -default-background-color 0x666666 -target-player=10.0 -optimize=true
    
    /**
     * The main Application class of the application.
     */
    public class Application extends Root 
    {
        /**
         * Creates a new Application instance.
         */
        public function Application()
        {
            //
        }
       
        /**
         * The loader of the application.
         */
        public var loader:ApplicationLoader ;
        
        /**
         * The wait field.
         */
        public var field:TextField ;
        
        /**
         * Invoked when the display is added to the stage.
         */
        protected override function addedToStage( e:Event = null ):void
        {
            // stage
            
            stage.align                  = StageAlign.TOP_LEFT ; 
            stage.scaleMode              = StageScaleMode.NO_SCALE ;
            stage.showDefaultContextMenu = false ;
            
            // logging
            
            var noDebug : * = flashVars.getValue("debug") === "false" ;
            if ( !noDebug && !sos ) 
            {  
                sos = new SOSTarget( getClassPackage(this) , 0xA2A2A2 ) ;
                sos.filters       = ["*"] ;
                sos.level         = LoggerLevel.ALL ;
                sos.includeLines  = true  ;
                sos.includeTime   = true  ;
            }
            
            // IoC
            
            var contextFile:String = flashVars.getValue("contextFile") || "application.eden" ;
            var contextPath:String = flashVars.getValue("contextPath") || "context/" ;
            
            loader = new ApplicationLoader(contextFile, contextPath) ;
            
            loader.finishIt.connect( finish ) ; 
            loader.startIt.connect( start ) ;
            
            loader.root  = this ;
            loader.stage = stage ;
            
            loader.run() ;
        }
        
        /**
         * Invoked when the IoC factory is initialized.
         */
        protected function finish( action:Action ):void
        {
            logger.info( this + " finish" ) ; 
            removeChild(field) ;
            field = undefined ;
        }
        
        /**
         * Invoked when the IoC factory start the initialization.
         */
        protected function start( action:Action ):void
        {
            logger.info( this + " start" ) ;
            
            var progressColor:Number   = ( flashVars.getValue("progressColor", new EdenEvaluator()) || 0x666666 ) as Number  ;
            var progressMessage:String = flashVars.getValue("progressMessage") || "Loading in progress..."  ;
            
            field                   = new TextField() ;
            field.autoSize          = TextFieldAutoSize.LEFT ;
            field.defaultTextFormat = new TextFormat( "Verdana", 12, progressColor, true ) ;
            field.selectable        = false ;
            field.x                 = 10 ;
            field.y                 = 10 ;
            field.text              = progressMessage ;
            
            addChild(field) ;
        }
        
        ////////////// Linkage enforcer
        
        // graphics
        
        LineStyle ; FillStyle ;
        
        // vegas
        
        MapModelObject ;
        NetServerConnection ; NetStreamExpert ;
        
        // lunas
        
        DashboardButton ;
        LightFrameLabelButton ;
        AutoScrollContainer ;
        
        // system
        
        FrontController ;
        
        // evideo
        
        FullScreen ; SwitchFullScreen ; VideoOut ; VideoOver ;
        AddVideo   ; ChangeVideo  ;
        MetaDataChange ; MuteStream ; PauseStream  ; PlayStream ; ProgressStream ; 
        SeekStream ; StatusStream ; UnmuteStream ;
        
        Body ; Footer ; VideoList ; VideoProtector ; Wallpaper ;
        
        InitApplication ;
    }
}
